/*
 * stdio.h
 *
 * Copyright (C) 2017 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _STDIO_H_
#define _STDIO_H_

#include <mlcrt.h>
#include <stdlib.h>
#include <stdarg.h>

#define EOF -1

struct __CRT_PRIVATE(file);
typedef struct __CRT_PRIVATE(file) FILE;

extern FILE *__CRT_PUBLIC(stdin);
extern FILE *__CRT_PUBLIC(stdout);
extern FILE *__CRT_PUBLIC(stderr);

int __CRT_PUBLIC(fgetc)(FILE *stream);
int __CRT_PUBLIC(fputc)(int c, FILE *stream);
char *__CRT_PUBLIC(fgets)(char *s, int size, FILE *stream);
int __CRT_PUBLIC(fputs)(const char *s, FILE *stream);
int __CRT_PUBLIC(ungetc)(int c, FILE *stream);
char *__CRT_PUBLIC(gets)(char *s);
int __CRT_PUBLIC(puts)(const char *s);

static inline int __CRT_PUBLIC(getc)(FILE *stream)
{
    return __CRT_PUBLIC(fgetc)(stream);
}

static inline int __CRT_PUBLIC(getchar)(void)
{
    return __CRT_PUBLIC(fgetc)(__CRT_PUBLIC(stdin));
}

static inline int __CRT_PUBLIC(putc)(int c, FILE *stream)
{
    return __CRT_PUBLIC(fputc)(c, stream);
}

static inline int __CRT_PUBLIC(putchar)(int c)
{
    return __CRT_PUBLIC(fputc)(c, __CRT_PUBLIC(stdout));
}

int __CRT_PUBLIC(fgetc_unlocked)(FILE *stream);
int __CRT_PUBLIC(fputc_unlocked)(int c, FILE *stream);
char *__CRT_PUBLIC(fgets_unlocked)(char *s, int size, FILE *stream);
int __CRT_PUBLIC(fputs_unlocked)(const char *s, FILE *stream);

static inline int __CRT_PUBLIC(getc_unlocked)(FILE *stream)
{
    return __CRT_PUBLIC(fgetc_unlocked)(stream);
}

static inline int __CRT_PUBLIC(getchar_unlocked)(void)
{
    return __CRT_PUBLIC(fgetc_unlocked)(__CRT_PUBLIC(stdin));
}

static inline int __CRT_PUBLIC(putc_unlocked)(int c, FILE *stream)
{
    return __CRT_PUBLIC(fputc_unlocked)(c, stream);
}

static inline int __CRT_PUBLIC(putchar_unlocked)(int c)
{
    return __CRT_PUBLIC(fputc)(c, __CRT_PUBLIC(stdout));
}

FILE *__CRT_PUBLIC(fopen)(const char *pathname, const char *mode);
FILE *__CRT_PUBLIC(fdopen)(int fd, const char *mode);
FILE *__CRT_PUBLIC(fmemopen)(void *buf, size_t size, const char *mode);
int __CRT_PUBLIC(fclose)(FILE *stream);

#define BUFSIZ 4096
#define _IOFBF 0
#define _IOLBF 1
#define _IONBF 2

int __CRT_PUBLIC(setvbuf)(FILE *stream, char *buf, int mode, size_t size);
void __CRT_PUBLIC(setbuf)(FILE *stream, char *buf);
void __CRT_PUBLIC(setbuffer)(FILE *stream, char *buf, size_t size);
void __CRT_PUBLIC(setlinebuf)(FILE *stream);

int __CRT_PUBLIC(printf)(const char *format, ...);
int __CRT_PUBLIC(fprintf)(FILE *stream, const char *format, ...);
int __CRT_PUBLIC(dprintf)(int fd, const char *format, ...);
int __CRT_PUBLIC(sprintf)(char *str, const char *format, ...);
int __CRT_PUBLIC(snprintf)(char *str, size_t size, const char *format, ...);
int __CRT_PUBLIC(vprintf)(const char *format, va_list ap);
int __CRT_PUBLIC(vfprintf)(FILE *stream, const char *format, va_list ap);
int __CRT_PUBLIC(vdprintf)(int fd, const char *format, va_list ap);
int __CRT_PUBLIC(vsprintf)(char *str, const char *format, va_list ap);
int __CRT_PUBLIC(vsnprintf)(char *str, size_t size, const char *format, va_list ap);

#endif
